<div class="row home">
    <div class="span7">
        <div id="homeSlider" class="carousel slide is-block">

            <?php
            $slideshow_amount = count($slideshow_list);
            if ($slideshow_amount > 1):
                ?>
                <ol class="carousel-indicators">
                    <?php for ($i = 0; $i < $slideshow_amount; $i++): ?>
                        <li data-target="#homeSlider" data-slide-to="<?php echo $i ?>" <?php echo $i == 0 ? 'class="active"' : '' ?>></li>
                    <?php endfor; ?>
                </ol>

            <?php endif; ?>

            <!-- Carousel items -->
            <div class="carousel-inner">
                <?php
                $i = 0;
                foreach ($slideshow_list as $slideshow):
                    ?>
                    <div class="<?php echo $i == 0 ? 'active' : '' ?> item">
                        <a href="<?php echo $slideshow['slideshow_url'] ?>" <?php echo $slideshow['new_window'] == '1' ? 'target="_blank"' : '' ?>><img src="<?php echo $slideshow['image_url'] ?>"/></a>
                    </div>
                    <?php
                    $i++;
                endforeach;
                ?>
            </div>
            <?php if ($slideshow_amount > 1): ?>
                <!-- Carousel nav -->
                <a class="carousel-control left" href="#homeSlider" data-slide="prev">&lsaquo;</a>
                <a class="carousel-control right" href="#homeSlider" data-slide="next">&rsaquo;</a>
            <?php endif; ?>
        </div>
    </div>
    <div class="span5">

        <?php if (is_null($user_id)): ?>
            <div class="is-block bottom profile-block" style="height:360px;overflow-y:auto;overflow-x:hidden">
                <a href="javascript:void(0)" onclick="login()" class="btn btn-primary btn-large btn-block"><i class="icon-facebook-sign"></i> Facebook Login</a>
                <script>
                    function login() {
                        FB.login(function() {
                            location.href = "<?php echo base_url() ?>fb_login?redirect=<?php echo base64_encode(current_url()) ?>";
                        }, {
                            scope: 'email,user_birthday'
                        });
                    }
                </script>

                <hr/>
                <div class="fb-like-box" data-href="http://www.facebook.com/pages/moohooooocom/143308545849364" data-width="360" data-show-faces="true" data-stream="false" data-header="false"></div>
            </div>
        <?php else: ?>
            <div class="is-block bottom profile-block" style="height:250px;overflow-y:scroll;overflow-x:hidden">

                <h5>ประวัติการโหวตล่าสุดของคุณ</h5>
                <?php
                if (!$recent_voted_list) {
                    ?>
                    <div class="alert alert-warning"><i class="icon icon-exclamation-sign"> คุณยังไม่มีการโหวตผู้เข้าแข่งรายใด</i></div>
                    <?php
                }

                foreach ($recent_voted_list as $candidate) {
                    ?>
                    <div class="recent_voted_wrapper">
                        <a href="<?php echo base_url() ?>contest/<?php echo hashids_encrypt($candidate['campaign_id']) ?>/<?php echo $candidate['candidate_number'] ?>">
                            <img src="https://graph.facebook.com/<?php echo $candidate['fb_id'] ?>/picture" class="pull-left avatar img-polaroid img-rounded"/>
                            <div class="description pull-left">
                                <?php echo '[#' . $candidate['candidate_number'] . '] - ' . $candidate['firstname'] . ' ' . $candidate['lastname'] ?><br/>
                                <span><b>ชื่อผลงาน</b>: <?php echo $candidate['campaign_join_title'] ?> </span><br/>
                                <span><b>จากการแข่งขัน</b>: <?php echo $candidate['campaign_title'] ?></span>

                            </div>
                            <div class="vote pull-left">
                                <?php
                                if ($candidate['positive_vote'] > 0) {
                                    echo '<div class="green">+' . $candidate['positive_vote'] . '</div>';
                                } else {
                                    echo '<div class="red">-' . $candidate['negative_vote'] . '</div>';
                                }
                                ?>
                            </div>
                        </a>
                        <div class="clearfix"></div>
                    </div>

                    <?php
                }
                ?>
            </div>

            <div class="is-block bottom profile-block" style="height:70px;overflow-y:hidden;overflow-x:hidden">
                <div class="fb-like-box" data-href="http://www.facebook.com/pages/moohooooocom/143308545849364" data-width="360" data-show-faces="false" data-stream="false" data-header="false"></div>
            </div>
        <?php endif; ?>

    </div>
</div>


<!-- Most Active Candidate-->
<?php
if (isset($active_candidate) && $active_candidate !== FALSE):
    ?>
    <div class="is-block bottom">
        <div class="row home">
            <div class="span7">
                <?php
                $register_content = unserialize($active_candidate['register_content']);
                if (!$user_id) {
                    $this->load->view('emergency_login');
                } else if ($active_candidate['candidate_vdo'] > 0) {
                    ?>
                    <div id="galleria">
                        <?php
                        foreach ($register_content as $content):
                            ?>
                            <a href="http://www.youtube.com/watch?v=<?php echo $content ?>"><span class="video">Youtube</span></a>
                            <?php
                        endforeach;
                        ?>
                    </div>
                    <style>
                        /* This rule is read by Galleria to define the gallery height: */
                        #galleria{height:500px}
                    </style>
                    <script>
                        $(function() {
                            Galleria.configure({
                                fullscreenDoubleTap: true,
                                autoplay: true,
                                transition: 'fade',
                                lightbox: true
                            });
                            Galleria.loadTheme('<?php echo base_url() ?>js/galleria/themes/classic/galleria.classic.min.js');
                            Galleria.run('#galleria');
                        });

                    </script>
                    <?php
                }
                elseif ($active_candidate['candidate_image'] > 0) {
                    ?>
                    <div id="galleria">
                        <?php
                        foreach ($register_content as $content):
                            ?>

                            <img src="<?php echo base_url() ?>uploads/register_image/<?php echo $active_candidate['id'] ?>/<?php echo $content ?>"/>
                            <?php
                        endforeach;
                        ?>
                    </div>
                    <style>
                        /* This rule is read by Galleria to define the gallery height: */
                        #galleria{height:500px}
                    </style>
                    <script>
                        $(function() {
                            Galleria.configure({
                                fullscreenDoubleTap: true,
                                autoplay: true,
                                transition: 'fade',
                                lightbox: true
                            });
                            Galleria.loadTheme('<?php echo base_url() ?>js/galleria/themes/classic/galleria.classic.min.js');
                            Galleria.run('#galleria');
                        });

                    </script>
                    <?php
                }
                else {

                    foreach ($register_content as $content):
                        ?>
                        <div class="well" style="background:white;">
                            <?php echo $content ?>
                        </div>
                        <?php
                    endforeach;
                }
                ?>
            </div>

            <!-- Active Candidate Description -->
            <div class="span5" style="width:350px;">
                <h5><?php echo $active_candidate['candidate_title'] ?></h5>
                <hr/>
                <div class="candidate-stat" style="width:100%">
                    <center>
                        <?php
                        $positive = $active_candidate['sum_positive_score'] == 0 ? 1 : $active_candidate['sum_positive_score'];
                        $negative = $active_candidate['sum_negative_score'] == 0 ? 1 : $active_candidate['sum_negative_score'];
                        $sum = $positive + $negative;
                        $positive_bar = floor($positive * 100 / $sum);
                        $negative_bar = floor($negative * 100 / $sum);
                        ?>

                        <div class="progress progress-striped active">
                            <div class="bar bar-success" style="width: <?php echo $positive_bar . '%' ?>"></div>
                            <div class="bar bar-danger" style="width: <?php echo $negative_bar . '%' ?>"></div>
                        </div>

                        <span>
                            <i class="icon-eye-open"></i> <?php echo number_format($active_candidate['sum_view']) ?>
                        </span>

                        <span>
                            <i class="icon-thumbs-up"></i> <?php echo number_format($active_candidate['sum_positive_score']) ?>
                        </span>
                        <span>
                            <i class="icon-thumbs-down"></i> <?php echo number_format($active_candidate['sum_negative_score']) ?>
                        </span>
                        <span>
                            <i class="icon-bolt"></i>  <?php echo number_format($active_candidate['sum_wilson_score'], 2) ?>
                        </span>
                    </center>

                </div>
                <hr/>
                <div class="candidate-share pull-left">
                    <div class="fb-like" data-href="<?php echo base_url() ?>contest/<?php echo hashids_encrypt($active_candidate['campaign_id']) ?>/<?php echo $active_candidate['candidate_number'] ?>" data-send="true" data-layout="box_count" data-width="450" data-show-faces="false"></div>
                </div>



                <div class="candidate-vote pull-left">
                    <div class="candidate-info">
                        <img src="https://graph.facebook.com/<?php echo $active_candidate['fb_id'] ?>/picture" class="pull-left avatar img-polaroid" alt="<?php echo $fullname ?>"/>
                        <div class="candidate-fullname pull-left">
                            <?php echo '[#' . $active_candidate['candidate_number'] . '] - ' . $active_candidate['firstname'] . ' ' . $active_candidate['lastname'] ?><br/>
                            <span>เมื่อ: <?php echo date('d-m-Y เวลา H:i:s', $active_candidate['created_at']) ?></span>
                        </div>
                    </div>
                    <div class="clearfix"></div>
                    <div class="candidate-vote-button btn-group">
                        <a href="javascript:void(0)" class="btn btn-success vote-up-button vote-button" <?php echo $is_voted ? 'disabled="disabled"' : '' ?>><i class="icon-thumbs-up"></i> โหวตชอบ</a>
                        <a href="javascript:void(0)" class="btn btn-danger vote-down-button vote-button"  <?php echo $is_voted ? 'disabled="disabled"' : '' ?>><i class="icon-thumbs-down"></i> โหวตไม่ชอบ</a>
                    </div>
                    <?php
                    if (!$is_voted):
                        ?>
                        <script>
                            $(function() {
                                $('.vote-up-button').click(function() {
                                    $('.vote-button').button('loading');
                                    $.post('<?php echo base_url() ?>ajax/do_vote_up', {
                                        'hash': '<?php echo hashids_encrypt($active_candidate['campaign_id']) ?>',
                                        'candidate_id': '<?php echo $active_candidate['candidate_id'] ?>'
                                    }, function() {


                                        $('.vote-button').button('reset');

                                        setTimeout(function() {
                                            $('.vote-button').attr('disabled', 'disabled');
                                        }, 0);
                                    });
                                });

                                $('.vote-down-button').click(function() {
                                    $('.vote-button').button('loading');
                                    $.post('<?php echo base_url() ?>ajax/do_vote_down', {
                                        'hash': '<?php echo hashids_encrypt($active_candidate['campaign_id']) ?>',
                                        'candidate_id': '<?php echo $active_candidate['candidate_id'] ?>'
                                    }, function() {
                                        $('.vote-button').button('reset');
                                        setTimeout(function() {
                                            $('.vote-button').attr('disabled', 'disabled');
                                        }, 0);

                                    });
                                });
                            });
                        </script>
                        <?php
                    endif;
                    ?>
                </div>

                <div class="clearfix"></div>
                <hr/>



                <div class="candidate-description">
                    <?php echo nl2br(strip_tags($active_candidate['description'])) ?>
                </div>

            </div>
            <div class="clearfix"></div>
        </div>
    </div>
    <?php
endif;
?>

<div class="row home">

    <div class="span12">
        <div class="is-block bottom">

            <ul id="myTab" class="nav nav-tabs">
                <li class="active"><a href="#recent" data-toggle="tab">รายการแข่งขันล่าสุด</a></li>
                <li><a href="#coming" data-toggle="tab">รายการแข่งขันที่กำลังมา</a></li>
            </ul>


            <div id="myTabContent" class="tab-content">
                <div class="tab-pane fade active in" id="recent">
                    <?php
                    $this->load->helper('url');
                    if ($campaign_recent_list) {
                        foreach ($campaign_recent_list as $campaign) {
                            ?>
                            <style>
        <?php echo $campaign['css_preset'] ?>
                            </style>
                            <table class="<?php echo $campaign['css_class'] ?> table campaign-list-table">
                                <thead>
                                    <tr>
                                        <th colspan="4"><?php echo $campaign['title'] ?> 

                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td style="width: 100px;"><img src="<?php echo base_url() ?>timthumb.php?src=<?php echo base_url() ?>uploads/sponsor_logo/<?php echo $campaign['logo'] ?>" style="border-radius:5px;"/></td>
                                        <td style="text-align: left">
                                            <?php
                                            if ($campaign['candidate_vdo'] != 0) {
                                                echo '<i class="icon-film"></i> ส่งวีดีโอ Youtube จำนวน: ' . $campaign['candidate_vdo'] . ' คลิป';
                                            }
                                            if ($campaign['candidate_image'] != 0) {
                                                echo '<i class="icon-camera-retro"></i> ส่งรูปถ่ายจำนวน: ' . $campaign['candidate_image'] . ' รูป';
                                            }
                                            if ($campaign['candidate_text'] != 0) {
                                                echo '<i class="icon-file-alt"></i> ส่งเอกสารบทความจำนวน: ' . $campaign['candidate_text'] . ' ชิ้น';
                                            }
                                            ?>

                                            <?php
                                            echo $this->campaign->campaign_reward_title_display(
                                                    array(
                                                        'positive_score_reward' => $campaign['positive_score_reward'],
                                                        'view_score_reward' => $campaign['view_score_reward'],
                                                        'wilson_score_reward' => $campaign['wilson_score_reward'],
                                            ));
                                            ?>
                                        </td>
                                        <td style="width: 150px;">
                                            <?php
                                            echo $this->campaign->status_title_campaign_icon($campaign['status']);
                                            ?>
                                        </td>
                                        <td style="width: 250px;">
                                            <?php
                                            if ($campaign['status'] == 'stopped') {
                                                ?>
                                                <a href="<?php echo base_url() ?>contest/<?php echo hashids_encrypt($campaign['id']) ?>/announce" class="btn btn-success btn-block"><i class="icon-certificate"></i> ประกาศผลรางวัล</a>
                                                <?php
                                            }
                                            if ($campaign['status'] != 'pending') {
                                                ?>
                                                <a href="<?php echo base_url() ?>contest/<?php echo hashids_encrypt($campaign['id']) ?>" class="btn btn-danger btn-block"><i class="icon-home"></i> หน้าผู้เข้าแข่งขัน</a>
                                                <?php
                                            }
                                            if ($campaign['status'] == 'starting') {
                                                ?>
                                                <a href="<?php echo base_url() ?>contest/<?php echo hashids_encrypt($campaign['id']) ?>/register" class="btn btn-success btn-block"><i class="icon-pencil"></i> ลงสมัครแข่งขัน</a>
                                                <?php
                                            }
                                            ?>

                                            <a href="<?php echo base_url() ?>contest/<?php echo hashids_encrypt($campaign['id']) ?>/description" class="btn btn-primary btn-block"><i class="icon-zoom-in"></i> รายละเอียดการแข่งขัน</a>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                            <?php
                        }
                    } else {
                        ?>
                        <div class="alert alert-warning" style="margin-bottom:0">
                            <i class="icon icon-exclamation-sign"></i> ยังไม่มีกิจกรรมที่กำลังแข่งขันในขณะนี้
                        </div>
                        <?php
                    }
                    ?>
                </div>


                <div class="tab-pane fade in" id="coming">
                    <?php
                    $this->load->helper('url');
                    if ($campaign_coming_list) {
                        foreach ($campaign_coming_list as $campaign) {
                            ?>
                            <style>
        <?php echo $campaign['css_preset'] ?>
                            </style>
                            <table class="<?php echo $campaign['css_class'] ?> table campaign-list-table">
                                <thead>
                                    <tr>
                                        <th colspan="4"><?php echo $campaign['title'] ?> 

                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td style="width: 100px;"><img src="<?php echo base_url() ?>timthumb.php?src=<?php echo base_url() ?>uploads/sponsor_logo/<?php echo $campaign['logo'] ?>" style="border-radius:5px;"/></td>
                                        <td style="text-align: left">
                                            <?php
                                            if ($campaign['candidate_vdo'] != 0) {
                                                echo '<i class="icon-film"></i> ส่งวีดีโอ Youtube จำนวน: ' . $campaign['candidate_vdo'] . ' คลิป';
                                            }
                                            if ($campaign['candidate_image'] != 0) {
                                                echo '<i class="icon-camera-retro"></i> ส่งรูปถ่ายจำนวน: ' . $campaign['candidate_image'] . ' รูป';
                                            }
                                            if ($campaign['candidate_text'] != 0) {
                                                echo '<i class="icon-file-alt"></i> ส่งเอกสารบทความจำนวน: ' . $campaign['candidate_text'] . ' ชิ้น';
                                            }
                                            ?>

                                            <?php
                                            echo $this->campaign->campaign_reward_title_display(
                                                    array(
                                                        'positive_score_reward' => $campaign['positive_score_reward'],
                                                        'view_score_reward' => $campaign['view_score_reward'],
                                                        'wilson_score_reward' => $campaign['wilson_score_reward'],
                                            ));
                                            ?>
                                        </td>

                                        <td style="width: 250px;">
                                            <a href="<?php echo base_url() ?>contest/<?php echo hashids_encrypt($campaign['id']) ?>/description" class="btn btn-primary btn-block"><i class="icon-zoom-in"></i> รายละเอียดการแข่งขัน</a>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                            <?php
                        }
                    } else {
                        ?>
                        <div class="alert alert-warning" style="margin-bottom:0">
                            <i class="icon icon-exclamation-sign"></i> ยังไม่มีกิจกรรมที่กำลังมาในขณะนี้
                        </div>
                        <?php
                    }
                    ?>
                </div>
            </div>
        </div>
    </div>

</div>

<script>
    $(function() {
        $('.carousel').carousel();
    });
</script>


